# -*- coding: utf-8 -*-
# Project : flacon
# File : flac_encoder.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

from PyQt4.QtCore import *
import flaconglobal

from encoderConfigPage import EncoderConfigPage
from ogg_config_ui import Ui_oggConfigPage as UI

class OggConfig(EncoderConfigPage, UI):

   #****************************************************************
   #
   #****************************************************************
    @staticmethod
    def defaults():
        return {
            'Ogg/UseQuality':       True,
            'Ogg/Quality':          7,
            'Ogg/MinBitrate':       '',
            'Ogg/NormBitrate':      '',
            'Ogg/MaxBitrate':       '',
            'Ogg/ReplayGain':       flaconglobal.ReplayGainTypes.Disable,
        }


   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent):
        EncoderConfigPage.__init__(self, parent)
        self.setupUi(self)

        self.setLossyToolTip(self.oggQualitySpin)
        self.oggQualitySlider.setToolTip(self.oggQualitySpin.toolTip())
        self.oggQualityLabel.setToolTip(self.oggQualitySpin.toolTip())

        self.connect(self.oggQualitySlider, SIGNAL('valueChanged(int)'),  self.oggQualitySliderChanged)
        self.connect(self.oggQualitySpin, SIGNAL('valueChanged(double)'), self.oggQualitySpinChanged)
        self.fillReplayGainComboBox(self.oggGainCbx)

        self.loadCheckBox('Ogg/UseQuality',  self.oggUseQualityCheck)
        self.loadSpinBox( 'Ogg/Quality',     self.oggQualitySpin)
        self.loadComboBox('Ogg/MinBitrate',  self.oggMinBitrateCbx)
        self.loadComboBox('Ogg/NormBitrate', self.oggNormBitrateCbx)
        self.loadComboBox('Ogg/MaxBitrate',  self.oggMaxBitrateCbx)
        self.loadComboBox('Ogg/ReplayGain',  self.oggGainCbx)


   #****************************************************************
   #
   #****************************************************************
    def write(self):
        self.writeCheckBox('Ogg/UseQuality',  self.oggUseQualityCheck)
        self.writeSpinBox( 'Ogg/Quality',     self.oggQualitySpin)
        self.writeComboBox('Ogg/MinBitrate',  self.oggMinBitrateCbx)
        self.writeComboBox('Ogg/NormBitrate', self.oggNormBitrateCbx)
        self.writeComboBox('Ogg/MaxBitrate',  self.oggMaxBitrateCbx)
        self.writeComboBox('Ogg/ReplayGain',  self.oggGainCbx)


   #****************************************************************
   #
   #****************************************************************
    def oggQualitySliderChanged(self, value):
        val = value / 10.0
        self.oggQualitySpin.setValue(val)


   #****************************************************************
   #
   #****************************************************************
    def oggQualitySpinChanged(self, value):
        self.oggQualitySlider.setValue(value * 10)
        #       -1  0   1   2   3    4    5    6    7    8    9    10   fake
        kbps = [45, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 500, 500]
        x1 = int(value)
        y1=kbps[x1+1];
        y2=kbps[x1+2]
        bitrate = round((y2-y1+0.0) * (value - x1) + y1)
        self.oggQualityLabel.setText(QString("(~%1 kbps)").arg(bitrate))

        self.fillBitrateComboBox(self.oggMinBitrateCbx,  ['', 64, 80, 96, 128, 160, 196, 256, 350])
        self.fillBitrateComboBox(self.oggNormBitrateCbx, ['', 64, 80, 96, 128, 160, 196, 256, 350])
        self.fillBitrateComboBox(self.oggMaxBitrateCbx,  ['', 64, 80, 96, 128, 160, 196, 256, 350])
